/*
 * TileMapDialog.java
 *
 * Created on 1 mai 2007, 17:40
 *
 */

package courselog.maputils;

import courselog.CalibrateForm;
import ewe.fx.Color;
import ewe.fx.Dimension;
import ewe.fx.Font;
import ewe.fx.FontMetrics;
import ewe.fx.IImage;
import ewe.fx.Sound;
import ewe.sys.Convert;
import ewe.ui.Control;
import ewe.ui.ControlEvent;
import ewe.ui.Form;
import ewe.ui.Gui;
import ewe.ui.IKeys;
import ewe.ui.InputValidator;
import ewe.ui.KeyEvent;
import ewe.ui.mButton;
import ewe.ui.mInput;
import ewe.ui.mLabel;
import ewe.ui.mTextPad;


/**
 *
 * @author rigal
 */
public class TileMapDialog extends Form implements InputValidator {
    
    /** Divider value */
    public int valDivide=2;
    public int oW;
    public int oH;
    
    public mLabel mlOriginalSize;
    public mLabel mlTileSize;
    public mTextPad mtInfo;
    
//    public mInput kiNb;
    public KeyedInput kiNb;
    
    /** Creates a new instance of MapReferencePointdialog */
    public TileMapDialog(IImage img) {
        oW=img.getWidth();
        oH=img.getHeight();
        setTitle("Enter divider");
        mtInfo=new mTextPad();
        mtInfo.setAsStaticDisplay(true);
        mtInfo.setText("Divide a large bitmap into several smaller maps."+
                "Enter the number of divisions to do, they will be applied both to"+
                " horizontal and vertical dimensions");
//        kiNb=new mInput("                 ");
        kiNb=new KeyedInput("                 ");
        kiNb.validator=this;
        kiNb.setText(Convert.formatInt(valDivide));
        
        mlOriginalSize=new mLabel("Original size: "+oW+"x"+oH);
        Font fNorm=mlOriginalSize.getFont();
        
        int nHeight = fNorm.getSize();
        Font largeFont = fNorm.changeNameAndSize(null, nHeight*2);
        largeFont=largeFont.changeStyle(Font.BOLD);
        mlOriginalSize.setFont(largeFont);
        
        mlTileSize=new mLabel("Tile size : 0000x0000");
        mlTileSize.setFont(largeFont);
        addLast(mlOriginalSize);
        addLast(kiNb);
        addLast(mlTileSize);
        addLast(mtInfo);
        upDateLabel();
        
        //Gui.setOKCancel(this);
        if(this.ok==null) {
            this.ok=new mButton("OK","res/iconok.bmp",Color.White);
            //this.ok.addListener(this);
            addNext(this.ok);
        }
        
        if(this.cancel==null) {
            this.cancel=new mButton("CANCEL","res/iconcancel.bmp",Color.White);
            //this.cancel.addListener(this);
            addLast(this.cancel);
        }
        
//        Dimension dimP= Device.getScreenSize();
//        if(dimP.height>400) dimP.height=400;
//        if(dimP.width>400) dimP.width=400;
//        
//        mlOriginalSize.setPreferredSize((int)(dimP.width*0.8d),(int)(dimP.height*0.1d));
//        kiNb.setPreferredSize((int)(dimP.width*0.8d),(int)(dimP.height*0.1d));
//        mlTileSize.setPreferredSize((int)(dimP.width*0.8d),(int)(dimP.height*0.2d));
//        mtInfo.setPreferredSize((int)(dimP.width*0.8d),(int)(dimP.height*0.4d));
//        this.ok.setPreferredSize((int)(dimP.width*0.4d),(int)(dimP.height*0.2d));
//        this.cancel.setPreferredSize((int)(dimP.width*0.4d),(int)(dimP.height*0.2d));
        Gui.takeFocus(kiNb,Gui.ByRequest);
        
    }
    
    /** Update divider label value */
    public void upDateLabel() {
        mlTileSize.setText("Tile size : "+((int)(oW/valDivide)+"x"+
                ((int)(oH/valDivide))));
    }
    
    /** Check if a divider value is acceptable (image not lower than 20 pixels on one side and
     * divider value strictly positive.
     * @param v Value dividing the image size
     * @return true if the image is still more than 20 pixels on one side.
     */
    public boolean isValidDivider(int v) {
        if(v<=1) return false;
        if(((oW/v)<20)||((oH/v)<20)) return false;
        return true;
    }
    
// OVERRIDDEN METHODS
    
    @Override
    public void onControlEvent(ControlEvent ev) {
        /*
         if((ev.target==this.ok)||(ev.target==this.cancel)) {
            CalibrateForm.dbgL.addToLog("Event type="+ev.type);
        }
         */
        super.onControlEvent(ev);
    }
    
// INTERFACE METHODS
    
    public boolean isValidKeyPress(KeyEvent ev) {
        if((ev.key>='0')&&(ev.key<='9')) return true;
        if((ev.key==IKeys.LEFT)||(ev.key==IKeys.RIGHT)) return true;
        if((ev.key==IKeys.BACKSPACE)||(ev.key==IKeys.ENTER)) return true;
        if((ev.key==IKeys.DELETE)||(ev.key==IKeys.ESCAPE)) return true;
        if((ev.key==IKeys.END)||(ev.key==IKeys.HOME)) return true;
        return false;
    }
    
    public boolean isValidText(String text) {
        try {
            int newVal=Convert.parseInt(text);
            if(!isValidDivider(newVal)) return false;
            this.valDivide=newVal;
            upDateLabel();
        } catch (NumberFormatException ex) {
            return false;
        }
        return true;
    }
    
    /** Just to trap up and down keys in the input dialog to set to +/- 1 the divider.*/
    final class KeyedInput extends mInput {
        /** Contructor with string, only calls mInput constructor. */
        public KeyedInput(String txt) {
            super(txt);
            modify(0,Control.ShowSIP);
        }
        
        /** Special behaviour on up and down keys to increase/ decrease
         * the divider value.
         */
        public void onKeyEvent(KeyEvent ev) {
            if(ev.type==KeyEvent.KEY_PRESS) {
                if(ev.key==IKeys.UP) {
                    // Special behaviour : increase by one divider
                    int newVal=TileMapDialog.this.valDivide+1;
                    if(TileMapDialog.this.isValidDivider(newVal)) {
                        TileMapDialog.this.valDivide=newVal;
                        TileMapDialog.this.upDateLabel();
                        setText(Convert.formatInt(newVal));
                        return;
                    }
                    Sound.beep(Sound.MB_ICONEXCLAMATION);
                    return;
                }
                if(ev.key==IKeys.DOWN) {
                    // Special behaviour : increase by one divider
                    int newVal=TileMapDialog.this.valDivide-1;
                    if(TileMapDialog.this.isValidDivider(newVal)) {
                        TileMapDialog.this.valDivide=newVal;
                        TileMapDialog.this.upDateLabel();
                        setText(Convert.formatInt(newVal));
                        return;
                    }
                    Sound.beep(Sound.MB_ICONEXCLAMATION);
                    return;
                }
            }
            if((ev.key=='p')&&(ev.type==KeyEvent.KEY_RELEASE)) {
//            if((ev.key=='p')&&(ev.type==KeyEvent.KEY_PRESS)) {
                mButton mbOK=(mButton) TileMapDialog.this.ok;
                //mbOK.activate();
                mbOK.doAction(Control.ByRequest);
                mbOK.notifyAction();
            }
            if((ev.key=='q')&&(ev.type==KeyEvent.KEY_RELEASE)) {
//            if((ev.key=='q')&&(ev.type==KeyEvent.KEY_PRESS)) {
                mButton mbCancel=(mButton) TileMapDialog.this.cancel;
                //mbCancel.activate();
                mbCancel.doAction(Control.ByRequest);
                mbCancel.notifyAction();
            }
            super.onKeyEvent(ev);
        }
    }
}